<template>
  <div class="menu-create">
    <el-card class="box-card">
      <div slot="header">
        <h1>添加菜单</h1>
      </div>
      <el-form :model="form" status-icon ref="form" label-width="100px">
        <el-form-item label="菜单名称" prop="name">
          <el-input type="text" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" prop="parentId">
          <el-select v-model="form.parentId">
            <el-option :value="-1" label="无上级菜单"></el-option>
            <el-option
              v-for="(item, index) in parentMenuList"
              :key="`menu${index}`"
              :label="`${item.name}${index}`"
              :value="index"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单路径" prop="href">
          <el-input type="text" v-model="form.href"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="text" v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input type="text" v-model="form.icon"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="show">
          <el-switch v-model="form.show"></el-switch>
        </el-form-item>
        <el-form-item label="排序" prop="orderName">
          <el-input-number
            v-model="form.orderName"
            :min="1"
            :max="10"
          ></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" @click="onSubmit">
            提交
          </el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from "vue";
import { createMenu, getEditMenuInfo } from "@/services/menu";
import { Form } from "element-ui";
import type { menuItem } from "@/services/menu";
export default Vue.extend({
  name: "MenuCreate",
  data() {
    return {
      loading: false,
      form: {
        parentId: -1,
        name: "",
        href: "",
        icon: "",
        orderName: 1,
        description: "",
        show: false,
      },
      parentMenuList: [] as Array<menuItem>,
    };
  },

  created() {
    this.getMenuInfo();
  },

  methods: {
    async getMenuInfo() {
      const id = this.$route.query.id as string | undefined;
      const {
        data: {
          code,
          data: { parentMenuList, menuInfo },
        },
      } = await getEditMenuInfo(id || -1);
      if (code === "000000") {
        this.parentMenuList = parentMenuList;
        if (menuInfo) {
          this.form = menuInfo;
        }
      }
    },
    async onSubmit() {
      this.loading = true;
      try {
        const {
          data: { code },
        } = await createMenu(this.form);

        if (code === "000000") {
          this.$message.success("提交成功！");
          this.$router.back();
        }
      } catch {
        this.$message.error("提交失败！");
      }
      this.loading = false;
    },

    onReset() {
      (this.$refs.form as Form).resetFields();
    },
  },
});
</script>
